package com.example.xd.gamedemo.view_test;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import com.example.xd.gamedemo.R;

/**
 * @auther XuDan on 2020/5/14.
 */
public class MoveView extends View {
    public MoveView(Context context) {
        this(context, null);
    }

    public MoveView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MoveView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setBackgroundColor(getResources().getColor(R.color.colorAccent));
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        float last_x = event.getRawX();
        float last_y = event.getRawY();
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                break;
            case MotionEvent.ACTION_MOVE:
                float dx = last_x - event.getRawX();
                float dy = last_y - event.getRawY();
                setTranslationX(dx);
                setRotationY(dy);
                break;
            case MotionEvent.ACTION_UP:
                break;

        }
        last_x = event.getRawX();
        last_y = event.getRawY();

        return true;
    }
}
